package demo2;

public class Main {
    public static void main(String[] args) {
        Handler H1 = new Teacher();
        Handler H2 = new President();
        Handler H3 = new HeadMaster();

        H1.setSuccessor(H2);
        H2.setSuccessor(H3);

        int[] days = {9, 10 ,3 ,2 ,1 ,8 ,6 ,25 ,32};
        for (int day: days
             ) {
            H1.AskForLeave(day);
        }
    }
}

abstract class Handler{
    protected Handler successor;


    public abstract void AskForLeave(int day);//请假函数

    public void setSuccessor(Handler successor) {
        this.successor = successor;
    }
}

class Teacher extends Handler{

    @Override
    public void AskForLeave(int day) {
        if(day >= 0 && day< 3){
            System.out.println("老师批准请假:" + day +"天");
        }else if(this.successor !=null){
            this.successor.AskForLeave(day);
        }
    }
}
class President extends Handler{
    //院长类

    @Override
    public void AskForLeave(int day) {
        if(day>=3 && day<=7){
            System.out.println("院长批准请假:"+ day + "天");
        }else if(this.successor != null){
            this.successor.AskForLeave(day);
        }
    }
}
class HeadMaster extends Handler{
    //校长类

    @Override
    public void AskForLeave(int day) {
        if(day>=7 && day<=30){
            System.out.println("校长批准请假: "+day +"天");
        }else {
            System.out.println(day+"天"+"超过30天,不允许请假");
        }
    }
}